package com.shengqi.xinnongren.work.ui.user.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseMvpActivity;
import com.shengqi.xinnongren.common.md5.SafeUtils;
import com.shengqi.xinnongren.common.utils.LogUtil;
import com.shengqi.xinnongren.common.utils.ToastHelper;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.user.model.AddressBean;
import com.shengqi.xinnongren.work.ui.user.presenter.AddressPresenter;
import com.shengqi.xinnongren.work.ui.user.view.AddressView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 收货地址
 */
public class AddressActivity extends BaseMvpActivity<AddressView, AddressPresenter> implements AddressView {

    private static final String EXTRA_TYPE = "type";
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private List<AddressBean> mList;
    private AddressAdapter mAdapter;

    private boolean isChooseAddress = false;

    public static void startForResult(Context context, boolean chooseAddress, int requestCode) {
        Intent intent = new Intent(context, AddressActivity.class);
        intent.putExtra(EXTRA_TYPE, chooseAddress);
        ((Activity) context).startActivityForResult(intent, requestCode);
    }

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_address;
    }

    @Override
    protected void initView() {

        if (getIntent() != null) {
            isChooseAddress = getIntent().getBooleanExtra(EXTRA_TYPE, false);
        }

        mList = new ArrayList<>();
        mAdapter = new AddressAdapter(R.layout.item_address, mList);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        DividerItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        itemDecoration.setDrawable(ContextCompat.getDrawable(this, R.drawable.custom_divider));
        mRecyclerView.addItemDecoration(itemDecoration);
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (isChooseAddress) {
                Intent intent = new Intent();
                intent.putExtra("data", mList.get(position));
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.item_check:
                    mPresenter.setDefAddress(mList.get(position).addressId, mList.get(position).isDefault == 1 ? 0 : 1);
                    break;
                case R.id.item_ll_edit:
                    AddAddressActivity.start(this, 1, mList.get(position));
                    break;
                case R.id.item_ll_delete:
                    mPresenter.delAddress(mList.get(position).addressId);
                    break;
            }
        });

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getAddressList();
    }

    @Override
    protected AddressPresenter createPresenter() {
        return new AddressPresenter();
    }

    /**
     * 添加地址
     */
    @OnClick(R.id.tv_addAddress)
    void addAddress() {
        AddAddressActivity.start(this, 0, null);
    }

    @Override
    public void showLoadingView() {
        showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onNetworkError() {
        hideLoadingDialog();
    }

    @Override
    public void showAddressList(List<AddressBean> list) {
        mList.clear();
        mList.addAll(list);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void showDelRes(HttpRespond<String> respond) {
        if (respond.result == 1) {
            mPresenter.getAddressList();
            ToastHelper.getInstance(this).showSucceed(respond.message);
        } else
            ToastHelper.getInstance(this).showWarn(respond.message);
    }

    @Override
    public void showSetDefRes(HttpRespond<String> respond) {
        mPresenter.getAddressList();
        ToastHelper.getInstance(this).showSucceed(respond.message);
    }

    class AddressAdapter extends BaseQuickAdapter<AddressBean, BaseViewHolder> {

        public AddressAdapter(int layoutResId, @Nullable List<AddressBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, AddressBean item) {

            helper.setText(R.id.item_name, item.name);
            helper.setText(R.id.item_phone, item.mobile);
            helper.setTextColor(R.id.item_isDef, item.isDefault == 1 ? Color.parseColor("#82c120") : Color.parseColor("#878787"));
            helper.setText(R.id.item_address, item.province + item.city + item.county + item.address);
            helper.setChecked(R.id.item_check, item.isDefault == 1);

            helper.addOnClickListener(R.id.item_check);
            helper.addOnClickListener(R.id.item_ll_edit);
            helper.addOnClickListener(R.id.item_ll_delete);

        }
    }


}
